//
//  YSBindPhoneCtrl.m
//  Yueshi
//
//  Created by yueshi－mac1 on 16/1/25.
//  Copyright © 2016年 悦食中国. All rights reserved.
//

#import "YSBindPhoneCtrl.h"
#import "YSMineDataTool.h"

@interface YSBindPhoneCtrl ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTextFiled;
@property (weak, nonatomic) IBOutlet UIButton *sendCodeBtn;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextFiled;
//@property (strong, nonatomic)  UIImageView *bgImgView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *phoneTopConst;
@property (weak, nonatomic) IBOutlet UILabel *tipsLabel;

@property (assign, nonatomic) NSTimer *countDownTimer;
@property (assign, nonatomic) NSInteger currentTime;
@property (assign, nonatomic) BOOL bindSuccess;
@end

@implementation YSBindPhoneCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    self.currentTime = 60;
    
//    _bgImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, SCREENW, SCREENH)];
//    [self.view insertSubview:_bgImgView belowSubview:self.scrollView];
//    if(SCREENH==480){// 4s 4
//        self.bgImgView.image = [UIImage imageNamed:@"bg_login_4.jpg"];
//    }else if(SCREENH==568){// 5s 5
//        self.bgImgView.image = [UIImage imageNamed:@"bg_login_5.jpg"];
//    }else if(SCREENH==667){// 6
//        self.bgImgView.image = [UIImage imageNamed:@"bg_login_6.jpg"];
//    }else if(SCREENH==736){// 6 plus
//        self.bgImgView.image = [UIImage imageNamed:@"bg_login_6p.jpg"];
//    }
    
//    [UIUtil dealPlaceHolderColor:self.phoneTextFiled color:[UIColor whiteColor]];
//    [UIUtil dealPlaceHolderColor:self.pwdTextFiled color:[UIColor whiteColor]];
    
    if(SCREENH<667){// 4s 4 5s 5
        self.phoneTopConst.constant = SCREENH/667.0 * self.phoneTopConst.constant;
    }
    
    self.phoneTextFiled.tintColor = ThemeColor;
    self.pwdTextFiled.tintColor = ThemeColor;
    
    NSString *tips = @"为了帐号安全 请绑定手机号\n绑定后可用手机号直接登录";
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:tips];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:7];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [tips length])];
    [self.tipsLabel setAttributedText:attributedString];
    [self.tipsLabel sizeToFit];
}

-(BOOL)shouldStatusBarLightContent{
    return NO;
}

- (IBAction)tapAction:(id)sender {
    [self.view endEditing:YES];
}

- (IBAction)tapSender:(UIButton *)sender {
    switch (sender.tag) {
        case 0://返回
            [self back:nil];
            break;
        case 3:{//确定
            if(ISBLANK(self.phoneTextFiled.text)){
                ShowTips(@"手机号为空");
                return;
            }
            
            if(ISBLANK(self.pwdTextFiled.text)){
                ShowTips(@"验证码为空");
                return;
            }
            
            if(!self.phoneTextFiled.text.isValidatePhone){
                ShowTips(@"非法手机号");
                return;
            }
            
            [YSMineDataTool bindingPhoneWithPhoneCode:self.phoneTextFiled.text validateCode:self.pwdTextFiled.text success:^(BaseResponse *result) {
                if (result.isSucc) {
                    [DefaultsHelper setObject:self.phoneTextFiled.text forKey:kUserPhone];
                    self.bindSuccess = YES;
                    [self back:nil];
                }else{
                     ShowEnterTips(result.data);
                }
            } failure:^(NSError *error) {
                 ShowTips(kRequestFailure);
            }];
            break;
        }
        case 5:{// 获取验证码
            if(ISBLANK(self.phoneTextFiled.text)){
                ShowTips(@"手机号为空");
                return;
            }
            [YSMineDataTool sendCodeWithParams:self.phoneTextFiled.text type:0 success:^(BaseResponse *result) {
                if(result.isSucc){
                    [self startCountDown];
                }else{
                    ShowEnterTips(result.data);
                }
            } failure:^(NSError *error) {
                ShowTips(kRequestFailure);
            }];
            break;
        }
        default:
            break;
    }
}

-(void)back:(NSNotification *)nortif{
    [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:kUpdatePhoneCodeNortif object:[NSNumber numberWithBool:self.bindSuccess]];
}

- (void)startCountDown {
    //处理倒计时
    NSString* time_str = [NSString stringWithFormat:@"%lds",self.currentTime];
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_countDownTimer forMode:NSDefaultRunLoopMode];
    [_sendCodeBtn setEnabled:NO];
    [_sendCodeBtn setTitle:time_str forState:UIControlStateDisabled];
}

-(void)timeFireMethod{
    if(self.currentTime <= 0){
        [_countDownTimer invalidate];
        [_sendCodeBtn setEnabled:YES];
        [_sendCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.currentTime = 60;
    }else{
        self.currentTime--;
        NSString *timeStr = [NSString stringWithFormat:@"%lds",self.currentTime];
        [_sendCodeBtn setTitle:timeStr forState:UIControlStateDisabled];
    }
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSString *phone =   [DefaultsHelper stringForKey:kUserPhone];
    [self.phoneTextFiled setText:phone];
}

-(void)dealloc{
    _countDownTimer = nil;
}

@end
